package com.yjd.bi.report.util;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.yjd.bi.report.enums.TimeType;
import com.yjd.bi.system.model.TimeDimensionModel;
import com.yjd.bi.system.service.ITimeDimensionService;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServiceHolder;

public class TimeDimensionUtil {
	private static List<TimeDimensionModel> list;
	private static LinkedHashMap<Long, TimeDimensionModel> map;
	private static ITimeDimensionService service;
	private static List<String> dailyDemention;

	public static ITimeDimensionService getService() {
		if (service == null)
			service = ServiceHolder.getService(ITimeDimensionService.class,
					"timeDimensionService",
					RpmServiceKeyEnum.RPM_BI_SERVICE_KEY_E);
		return service;
	}

	public static void main(String[] args) {
		Long daynow = DateUtil.getTimeDayByTimestamp(FrameUtil.getTime());
		if (dailyDemention == null) {
			dailyDemention = FrameUtil.newArrayList();
			for (int i = 0; i < 48; i++) {
				dailyDemention.add(DateUtil.timestampToDateString(daynow,
						"HHmmss"));
				if (i <= 1) {
					dailyDemention.set(i,
							dailyDemention.get(i).replace("12", "00"));
				}
				System.out.println(dailyDemention.get(i));
				daynow += (30 * 60);
			}
		}
	}

	private static void init() throws Exception {
		synchronized (TimeDimensionUtil.class) {
			List<TimeDimensionModel> times = getService().getModelList(
					FrameUtil.newHashMap("sort", "id"), DbWREnums.READ);
			map = (LinkedHashMap<Long, TimeDimensionModel>) FrameUtil
					.newLinkedHashMap(null);
			for (TimeDimensionModel model : times) {
				map.put(model.getId(), model);
			}
			Long daynow = DateUtil.getTimeDayByTimestamp(FrameUtil.getTime());
			if (dailyDemention == null) {
				dailyDemention = FrameUtil.newArrayList();
				for (int i = 0; i < 48; i++) {
					dailyDemention.add(DateUtil.timestampToDateString(daynow,
							"HH:mm"));
					if (i <= 1) {
						dailyDemention.set(i,
								dailyDemention.get(i).replace("12", "00"));
					}
					daynow += (30 * 60);
				}
			}
			list = times;
		}
	}

	public static List<String> getDailyDemention() throws Exception {
		if (dailyDemention == null) {
			synchronized (TimeDimensionUtil.class) {
				if (dailyDemention == null) {
					init();
				}
			}
		}
		return dailyDemention;
	}

	/**
	 * 获取字典所有数据
	 * 
	 * @return
	 * @throws Exception
	 */
	public static List<TimeDimensionModel> getAll() throws Exception {
		if (list == null) {
			synchronized (TimeDimensionUtil.class) {
				if (list == null) {
					init();
				}
			}
		}
		return list;
	}

	/**
	 * 获取字典所有数据
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Map<Long, TimeDimensionModel> getMap() throws Exception {
		if (map == null) {
			synchronized (TimeDimensionUtil.class) {
				if (map == null) {
					init();
				}
			}
		}
		return map;
	}

	/**
	 * 根据时间维度类型以及开始统计时间 获取对应统计区间列表 区间类型：[]
	 * 
	 * @param t
	 * @param timeFrom
	 * @return 返回的List<> ［start,end］
	 * @throws Exception
	 */
	public static List<List<Long>> getSearchRanges(TimeType t, Long timeFrom)
			throws Exception {
		TimeDimensionModel timeBase = getMap().get(timeFrom);
		int k = list.indexOf(timeBase);
		List<List<Long>> result = FrameUtil.newArrayList(null);
		if (t == TimeType.DAY) {
			for (int i = 0; i < t.getUnit(); i++) {
				TimeDimensionModel temp1 = list.get(k);
				k--;
				if (k == 0) {
					break;
				}
				result.add(FrameUtil.newArrayList(temp1.getId(),
						temp1.getId()));
			}
		}
		if (t == TimeType.WEEK) {
			for (int i = 0; i < t.getUnit(); i++) {
				TimeDimensionModel temp1 = list.get(k);
				k--;
				while (temp1.getW().intValue() == list.get(k).getW().intValue()) {
					if (k == 0) {
						break;
					}
					k--;
				}
				result.add(FrameUtil.newArrayList(list.get(k+1).getId(),
						temp1.getId()));
			}
		}
		if (t == TimeType.MONTH) {
			for (int i = 0; i < t.getUnit(); i++) {
				TimeDimensionModel temp1 = list.get(k);
				k--;
				while (temp1.getM().intValue() == list.get(k).getM().intValue()) {
					if (k == 0) {
						break;
					}
					k--;
				}
				result.add(FrameUtil.newArrayList(list.get(k+1).getId(),
						temp1.getId()));
			}
		}
		return result;
	}

}
